package com.example.contactsbackup;

import java.io.BufferedReader;
import java.io.FileReader;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;

import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;

public class ToXml {

	public static final String ELE_CONTACTDETAILS = "ContactDetails";
	 public static final String ELE_CONTACT = "Contact";
	 public static final String ELE_CONTACT_NAME = "ContactName";
	 public static final String ELE_PHONE = "Phone";
	 public static final String ELE_EMAIL = "Email";
	 public static final String ELE_POSTAL_ADDRESS = "PostalAddress";

	 public static final String ELE_PHONE_NO = "PhoneNo";
	 public static final String ELE_TYPE = "Type";

	 public static final String ELE_MAILID = "MailId";

	 public static final String ELE_STREET = "Stree";
	 public static final String ELE_CITY = "City";
	 public static final String ELE_STATE = "State";
	 public static final String ELE_PINCODE = "Pincode";
	 public static final String ELE_COUNTRY = "Country";
	 
	 public static final String ELE_ORG="Orgazination";
	 public static final String ELE_COMPANY="Company";
	 public static final String ELE_STATUS="Status";
	 
	 public static final String ELE_IM="IM";
	 public static final String ELE_IMNAME="IMName";
	 
	 BufferedReader in;
	 StreamResult out;
	 TransformerHandler th;
	 AttributesImpl atts;

	 private String filePath="";
	 
	 public ToXml(String path){
	  this.filePath=path;
	  out = new StreamResult(filePath);
	 }
	 
	 public void doit() {
		  try {
		   in = new BufferedReader(new FileReader("C:\\pdf.txt"));
		   
		   initXML();
		   String str;
		   /*while ((str = in.readLine()) != null) {
		    process(str);
		   }*/
		   
		   startContactElement();
		   createContactNameNode("My Name");
		   createPhoneNode("90940349034", "work");
		   createEmailNode("abc@gmail.com", "Home");
		   createPostalAddressNode("Home", "street-1", "RAjkot", "gujarat", "4848374", "India");
		   endContactElement();
		   in.close();
		   closeXML();
		  } catch (Exception e) {
		   e.printStackTrace();
		  }
		 }
	 
	 public void initXML() throws ParserConfigurationException,
	   TransformerConfigurationException, SAXException {
	  SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory
	    .newInstance();

	  th = tf.newTransformerHandler();
	  Transformer serializer = th.getTransformer();
	  serializer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
	  serializer.setOutputProperty(
	    "{http://xml.apache.org/xslt}indent-amount", "4");
	  serializer.setOutputProperty(OutputKeys.INDENT, "yes");
	  th.setResult(out);
	  th.startDocument();
	  atts = new AttributesImpl();
	  th.startElement("", "", ELE_CONTACTDETAILS, atts);
	 }
	
	 public void closeXML() throws SAXException {
		  th.endElement("", "", ELE_CONTACTDETAILS);
		  th.endDocument();
		 }
	 
	 public void startContactElement() throws SAXException {
		  atts.clear();
		  th.startElement("", "", ELE_CONTACT, atts);
		 }
	 
	 public void endContactElement() throws SAXException {
		  atts.clear();
		  th.endElement("", "", ELE_CONTACT);
		 }
	 
	 public void createContactNameNode(String name) throws SAXException {
		  th.startElement("", "", ELE_CONTACT_NAME, atts);
		  th.characters(name.toCharArray(), 0, name.length());
		  th.endElement("", "", ELE_CONTACT_NAME);
		 }
	 
	 public void createPhoneNode(String number, String type) throws SAXException {

		  th.startElement("", "", ELE_PHONE, atts);

		  th.startElement("", "", ELE_PHONE_NO, atts);
		  th.characters(number.toCharArray(), 0, number.length());
		  th.endElement("", "", ELE_PHONE_NO);

		  th.startElement("", "", ELE_TYPE, atts);
		  th.characters(type.toCharArray(), 0, type.length());
		  th.endElement("", "", ELE_TYPE);

		  th.endElement("", "", ELE_PHONE);

		 }
	 
	 public void createEmailNode(String mailid, String type) throws SAXException {

		  th.startElement("", "", ELE_EMAIL, atts);

		  th.startElement("", "", ELE_MAILID, atts);
		  th.characters(mailid.toCharArray(), 0, mailid.length());
		  th.endElement("", "", ELE_MAILID);

		  th.startElement("", "", ELE_TYPE, atts);
		  th.characters(type.toCharArray(), 0, type.length());
		  th.endElement("", "", ELE_TYPE);

		  th.endElement("", "", ELE_EMAIL);
		 }
	 
	 public void createPostalAddressNode(String type, String street,
			   String city, String state, String pincode, String country)
			   throws SAXException {

			  th.startElement("", "", ELE_POSTAL_ADDRESS, atts);

			  th.startElement("", "", ELE_TYPE, atts);
			  th.characters(type.toCharArray(), 0, type.length());
			  th.endElement("", "", ELE_TYPE);

			  th.startElement("", "", ELE_STREET, atts);
			  th.characters(street.toCharArray(), 0, street.length());
			  th.endElement("", "", ELE_STREET);

			  th.startElement("", "", ELE_CITY, atts);
			  th.characters(city.toCharArray(), 0, city.length());
			  th.endElement("", "", ELE_CITY);

			  th.startElement("", "", ELE_STATE, atts);
			  th.characters(state.toCharArray(), 0, state.length());
			  th.endElement("", "", ELE_STATE);

			  th.startElement("", "", ELE_PINCODE, atts);
			  th.characters(pincode.toCharArray(), 0, pincode.length());
			  th.endElement("", "", ELE_PINCODE);

			  th.startElement("", "", ELE_COUNTRY, atts);
			  th.characters(country.toCharArray(), 0, country.length());
			  th.endElement("", "", ELE_COUNTRY);

			  th.endElement("", "", ELE_POSTAL_ADDRESS);

			 }
	 
	 public void createOrganizationNode(String company, String status) throws SAXException {

		  th.startElement("", "", ELE_ORG, atts);

		  th.startElement("", "", ELE_COMPANY, atts);
		  th.characters(company.toCharArray(), 0, company.length());
		  th.endElement("", "", ELE_COMPANY);

		  th.startElement("", "", ELE_STATUS, atts);
		  th.characters(status.toCharArray(), 0, status.length());
		  th.endElement("", "", ELE_STATUS);

		  th.endElement("", "", ELE_ORG);

		 }
	 
	 public void createIMNode(String ImName, String type) throws SAXException {

		  th.startElement("", "", ELE_IM, atts);

		  th.startElement("", "", ELE_IMNAME, atts);
		  th.characters(ImName.toCharArray(), 0,ImName.length());
		  th.endElement("", "", ELE_IMNAME);

		  th.startElement("", "", ELE_TYPE, atts);
		  th.characters(type.toCharArray(), 0, type.length());
		  th.endElement("", "", ELE_TYPE);

		  th.endElement("", "", ELE_IM);

		 }

}
